//
//  MeCollectionViewController.m
//  高仿萝卜书摘
//
//  Created by 谢俊逸 on 2016/11/25.
//  Copyright © 2016年 谢俊逸. All rights reserved.
//

#import "MeCollectionViewController.h"
#import "CollectionViewCell.h"
#import "XJYHeader.h"
#import "MeFooterCollectionReusableView.h"
#import "MeHeaderCollectionReusableView.h"
#import "MeCollectionTopView.h"
#import "MeCollectionHeaderView.h"
#import "XLPlainFlowLayout.h"


@interface MeCollectionViewController ()

@end

@implementation MeCollectionViewController

static NSString * const MeCollectionViewCellReuseIdentifier = @"MeCollectionViewCellReuseIdentifier";
static NSString * const MeCollectionViewFooterReuseIdentifier = @"MeCollectionViewFooterReuseIdentifier";
static NSString * const MeCollectionViewHeaderReuseIdentifier = @"MeCollectionViewHeaderReuseIdentifier";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我";
    //self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupNavigationBar];
    [self setupCollectionView];
    

    //清除选择
    //self.clearsSelectionOnViewWillAppear = NO;
    
    
#pragma mark  Register cell classes
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:MeCollectionViewCellReuseIdentifier];
    
    [self.collectionView registerClass:[MeFooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:MeCollectionViewFooterReuseIdentifier];
    
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([MeHeaderCollectionReusableView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:MeCollectionViewHeaderReuseIdentifier];
    
    
    
    // Do any additional setup after loading the view.
}

- (void)setupNavigationBar{
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(leftBarButtonItemClicked:)];
    leftBarButtonItem.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
    //自定义rightBarButtonItem
    UIButton *rightBarButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [rightBarButton setImage:[UIImage imageNamed:@"SettingsIcon"] forState:UIControlStateNormal];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBarButton];
    //UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"SettingsIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemClicked:)];
    rightBarButtonItem.tintColor = [UIColor lightGrayColor];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    
}

- (void)setupCollectionView{
    XLPlainFlowLayout *flowLayout = ({
        flowLayout = [[XLPlainFlowLayout alloc]init];
        flowLayout.itemSize = [FrameAutoScaleLFL CGSizeLFLMakeWidth:173 hight:300];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        CGFloat margin = 8;
        flowLayout.sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin);
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.minimumLineSpacing = 5;
#pragma mark -- 头尾部大小设置
        //设置头部并给定大小
        [flowLayout setHeaderReferenceSize:CGSizeMake(ScreenW, 50)];
        //设置尾部并给定大小
        [flowLayout setFooterReferenceSize:CGSizeMake(ScreenW, 40)];
        //flowLayout.footerReferenceSize
        flowLayout;
    });
    self.collectionView.alwaysBounceVertical = YES;
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.backgroundColor = ColorWithRGBA(235, 235, 235, 1);
    self.collectionView.contentInset = UIEdgeInsetsMake(130, 0, 0, 0);
    MeCollectionHeaderView *HeaderView = [[MeCollectionHeaderView alloc]initWithFrame:CGRectMake(0, -130, self.collectionView.frame.size.width, 130)];
    
    [self.collectionView addSubview:HeaderView];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MeCollectionViewCellReuseIdentifier forIndexPath:indexPath];
    
    // Configure the cell
    
    return cell;
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view = nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        MeHeaderCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:MeCollectionViewHeaderReuseIdentifier forIndexPath:indexPath];
        view = headView;
    }
    if (kind == UICollectionElementKindSectionFooter) {
        MeFooterCollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:MeCollectionViewFooterReuseIdentifier forIndexPath:indexPath];
        view = footView;
    }
    return view;
    
}

#pragma mark BarButtonItemClicked

- (void)leftBarButtonItemClicked:(UIBarButtonItem *)leftBarButtonItem{
    NSLog(@"leftBarButtonItemClicked");
}

- (void)rightBarButtonItemClicked:(UIBarButtonItem *)rightBarButtonItem{
    NSLog(@"rightBarButtonItemClicked");
    
    
}
#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
